package com.rtype.GPSilencer.helpers;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import com.rtype.GPSilencer.R;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public abstract class BaseDbAdapter {

	private static final String TAG = "BaseDbAdapter";
	private static final String DATABASE_NAME = "appdata";
	private static final int DATABASE_VERSION = 1;
	
	protected DatabaseHelper mDbHelper;
	protected SQLiteDatabase mDb;
	protected final Context mCtx;
	
	public static final String PROFILE_CREATE = "CREATE TABLE profile (_id integer primary key autoincrement, trigger_type integer not null, name text not null)";
	public static final String TRIGGER_TYPE_CREATE = "CREATE TABLE trigger_type (_id integer primary key autoincrement, name text not null)";
	public static final String TRIGGER_ITEM_CREATE = "CREATE TABLE trigger_item (_id integer primary key autoincrement, attribute_type_id integer not null, name text not null, description text)";
	public static final String TRIGGER_ITEM_VALUE_CREATE = "CREATE TABLE trigger_item_value (_id integer primary key autoincrement, attribute_id integer not null, profile_id integer not null, value text not null)";
	
	public static final String[] DB_CREATE = new String[] { PROFILE_CREATE, TRIGGER_TYPE_CREATE, TRIGGER_ITEM_CREATE, TRIGGER_ITEM_VALUE_CREATE };
	
	// Types to be created to the trigger_type table
	public static final String TRIGGER_TYPE_LOCATION = "location";
	public static final String TRIGGER_TYPE_TIME = "time";
	public static final String[] TRIGGER_TYPES = new String[] { TRIGGER_TYPE_LOCATION, TRIGGER_TYPE_TIME };

	// Items to be created to the trigger_item table
	public static final String[] TRIGGER_ITEM_LATITUDE = new String[] { TRIGGER_TYPE_LOCATION, "latitude", "" };
	public static final String[] TRIGGER_ITEM_LONGITUDE = new String[] { TRIGGER_TYPE_LOCATION, "longitude", "" };
	public static final String[] TRIGGER_ITEM_RADIUS = new String[] { TRIGGER_TYPE_LOCATION, "radius", "" };
	
	
	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// Create statements for tables here
			for (int i = 0; i < DB_CREATE.length; i++) {
				db.execSQL(DB_CREATE[i]);
			}
			initializeDatabase(db);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// Drop upgradable tables
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS profile");
            db.execSQL("DROP TABLE IF EXISTS trigger_type");
            
            onCreate(db);
		}
		
		public void initializeDatabase(SQLiteDatabase db) {
			for (int i = 0; i < TRIGGER_TYPES.length; i++) {
				ContentValues values = new ContentValues();
				values.put("name", TRIGGER_TYPES[i]);
				db.insert("trigger_type", null, values);
			}
		}
	}
	
	public BaseDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

	public BaseDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }
    


}
